/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#ifndef CORE_ACSERROR_H_
#define CORE_ACSERROR_H_

#include <string>

#include "AcsResponse.h"
#include "transform/UnmarshallerContext.h"

namespace aliyun {

class AcsError : public AcsResponse {
 public:
  typedef std::string string;

  const string& getErrorCode() const {
    return errorCode_;
  }

  void setErrorCode(const string& errorCode) {
    errorCode_ = errorCode;
  }

  const string& getErrorMessage() const {
    return errorMessage_;
  }

  void setErrorMessage(const string& errorMessage) {
    errorMessage_ = errorMessage;
  }

  const string& getRequestId() const {
    return requestId_;
  }

  void setRequestId(const string& requestId) {
    requestId_ = requestId;
  }

  static AcsError unmarshall(const UnmarshallerContext& context) {
    AcsError error;
    error.errorCode_ = context.getHttpStatus();
    error.requestId_ = getValue(context.getResponseMap(), "Error.RequestId");
    error.errorCode_ = getValue(context.getResponseMap(), "Error.Code");
    error.errorMessage_ = getValue(context.getResponseMap(), "Error.Message");
    return error;
  }

 private:
  static string getValue(const std::map<string, string>& dict, string key) {
    std::map<string, string>::const_iterator pos = dict.find(key);
    if (pos != dict.end()) {
      return pos->second;
    }
    return "";
  }

  int statusCode_;
  string errorCode_;
  string errorMessage_;
  string requestId_;
};

}  // namespace aliyun

#endif  // CORE_ACSERROR_H_
